package comp6231.project.common;
/**
 * Distributed Retail System
 * Sep 30, 2012 
 */

import java.util.concurrent.Semaphore;

/**
 * @author dimitri.tiago
 *
 * This class implements the semaphore synchronization construct
 */
public class BinarySemaphore 
{
	private final Semaphore countingSemaphore;

	public BinarySemaphore(boolean available) 
	{
		if (available) 
		{
			countingSemaphore = new Semaphore(1, true);
		} 
		else 
		{
			countingSemaphore = new Semaphore(0, true);
		}
	}

	public void Wait() 
	{
		try 
		{
			countingSemaphore.acquire();
		} 
		catch (InterruptedException e) 
		{
			// no-op
		}
	}

	public synchronized void Signal() 
	{
		if (countingSemaphore.availablePermits() != 1) 
		{
			countingSemaphore.release();
		}
	}
}